package mf.com.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import mf.com.Service.sys.SysMenuService;
import mf.com.Service.sys.SysUserService;
import mf.com.Service.sys.ValidateCodeService;
import mf.com.Utils.AuthContextUtil;
import mf.com.dto.system.LoginDto;
import mf.com.entity.system.SysMenu;
import mf.com.entity.system.SysUser;
import mf.com.vo.common.Result;
import mf.com.vo.system.LoginVo;
import mf.com.vo.system.ValidateCodeVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: lmf
 * @Create: 2024/3/23 14:40
 * Description:  首页控制层
 */
@RestController
@RequestMapping("/admin/system/index/")
@Tag(name="用户相关")
public class indexController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private ValidateCodeService validateCodeService;
    @GetMapping("getUserInfo")
    @Operation(summary = "获取用户信息")
    public Result<SysUser> getUserInfo() {

        return Result.success(AuthContextUtil.get());
    }

    //生成图片验证码
    @GetMapping(value = "/generateValidateCode")
    @Operation(summary = "生成验证码")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.getValidateCode();
        return Result.success(validateCodeVo);
    }

    @PostMapping("login")
    @Operation(summary = "用户登录")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        System.out.println(loginDto);
        return Result.success(sysUserService.userLogin(loginDto));
    }

    @GetMapping("menus")
    @Operation(summary = "根据token获取用户下对应菜单列表")
    public Result<List<SysMenu>> findForMenuByUserId(){
        return Result.success(sysMenuService.findForSubMenuByUserId());
    }

    @GetMapping("logout")
    @Operation(summary = "退出登录")
    public Result<Void> logout(@RequestHeader("token") String token){
        sysUserService.outLogin(token);
        return Result.success(null);
    }
}
